<!-- 一周项目一览表 -->
<template>
	<view>
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">库存</block></cu-custom>
		<view >
			<form>
				<view class="cu-form-group">
					<view class="title">货品名称:</view>
					<input name="input" v-model="goodsName"></input>
					<view class="title">批次号:</view>
					<input name="input" v-model="batchNum"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">货位:</view>
					<input name="input" v-model="goodsPosi"></input>
					<view class="title">规格:</view>
					<input name="input" v-model="spec"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">卷号:</view>
					<input name="input" v-model="innerBatch"></input>
					<button class='cu-btn bg-green shadow' @tap="queryClick()">查询</button>
				</view>
			</form>
		</view>
		<view class="margin-top">
			<refresh ref="refresh" @refresh='onRefresh'>
				<z-table :tableData='tableData' :columns='columns' :tableHeight="800"></z-table>
			</refresh>
		</view>
	</view>
</template>

<script>
	import { formatDate } from '@/utils/datetime.js'
	import { getDateRange } from '@/utils/index.js'
	import zTable from "@/components/z-table/z-table.vue"
	import refresh from "@/components/refresh/refresh.vue"
	import { mapGetters } from 'vuex'
	var _self
	var canvaPie=null
	export default {
		components: {
			zTable,
			refresh
		},
		computed: mapGetters(['loginType','user']),
		data() {
			return {
				tableData:[],
				columns:[
					// { title:'序号',key:'index',width: 80 },
					{ title:'客户名称',key:'clientName',width: 320 },
					{ title:'批次号',key:'batchNum',width: 220  },
					{ title:'货品名称',key:'goodsName',width: 260  },
					{ title:'规格',key:'spec',width: 260  },
					{ title:'型号',key:'model',width: 130  },
					{ title:'材质',key:'materialCode',width: 180  },
					{ title:'产地',key:'maker',width: 120  },
					{ title:'货位',key:'goodsPosi',width: 140  },
					{ title:'库存数量',key:'qty',width: 120  },
					{ title:'库存重量',key:'weight',width: 120  },
					{ title:'入库时间',key:'goodsInDat',width: 240  }
				],
				goodsName:'',
				batchNum:'',
				goodsPosi:'',
				spec:'',
				innerBatch: '',
				pageIndex: 1,
				pageSize: 10
			}
		},
		onLoad() {
			this.onRefresh()
		},
		methods: {
			onRefresh() {

				let params = {
					goodsName: this.goodsName,
					batchNum: this.batchNum,
					goodsPosi: this.goodsPosi,
					spec: this.spec,
					innerBatch: this.innerBatch,
					pageIndex: this.pageIndex,
					pageSize: this.pageSize,				 
				  }
				  
				  if (this.loginType === '客户') {
				  	params = {
						clientId: this.user.clientId,
				  		goodsName: this.goodsName,
				  		batchNum: this.batchNum,
				  		goodsPosi: this.goodsPosi,
				  		spec: this.spec,
						innerBatch: this.innerBatch,
				  		pageIndex: this.pageIndex,
				  		pageSize: this.pageSize					 
				  	  }
				  }
				  
				this.$minApi.getInStockGoodsDetail(params).then(res => {
				
					const data = res
					if (data) {
						
						let totalPage = data.totalCount
						let hasNext = this.tableData.length < totalPage
						
						if (data.list) {
							data.list.forEach( (item, index) => {
								item.index = (index +1)
								item.goodsInDat = formatDate(item.goodsInDat)
							})
						}

						if (this.pageIndex === 1) this.tableData = [] //如果是第一页需手动置空列表
						
						this.tableData.pop() //移除汇总行
						this.tableData = this.tableData.concat(data.list) //追加新数据
						
						const qtyValues = this.tableData.map(item => Number(item.qty))
						const qtyTotal = qtyValues.reduce((prev, curr) => { return prev + curr }, 0)
								
						const weightValues = this.tableData.map(item => Number(item.weight))
						const weightTotal = weightValues.reduce((prev, curr) => { return prev + curr }, 0)
						
						const total = {
						          clientName: '总计',
						          batchNum: '',
						          goodsName: '',
						          spec: '',
						          model: '',
						          materialCode: '',
						          maker: '',
								  goodsPosi: '',
								  qty: qtyTotal.toFixed(2),
								  weight: weightTotal.toFixed(2),
						          goodsInDat: ''
						        }

						this.tableData.push(total)

						this.pageIndex =  this.pageIndex + 1	// 页码, 默认从1开始
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					this.$refs.refresh.endAfter() //通知下拉刷新组件刷新已经完成
				})
				
			},
			queryClick() {
				this.pageIndex = 1
				this.onRefresh()
			},
			backedPage() {
				uni.switchTab({
					url: '/pages/main/index'
				})
			}
		}
	}
</script>

<style>
	/* .project-list+.project-list {
		margin-top: 2rpx !important;
	} */
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	
	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	.container {
		min-height: 400px !important;
	}
</style>
